// houseList.js
const requester = require('../../common/requester.js');
const EventEmitter = require('../../common/EventEmitter.js');
const config = require('../../common/config.js');
const util = require('../../utils/util.js');
const user = require('../../common/user.js');
const formIdInit = require('../../utils/formId.js');
const bookStorage = require('../../utils/bookStorage.js');
const scanCode = require('../../utils/scanCode.js');
const shareCodeHandler = require('../../utils/shareCodeHandler.js');
const $$onLoad = require('../../appLoaded/onLoad');
const app = getApp();

Page({
    autoPublish: false,
    data: {
        isAuthed: true,
        showAuthDialog: false,
        formConfig: {
            reportSubmit: true
        }
    },

    onLoad: function () {
        formIdInit({
            page: this
        });
        this.init();
        this.commonSetting();
        EventEmitter.addListener('publish', () => {
            this.autoPublish = true;
        })
    },

    commonSetting: function () {
        $$onLoad()
            .then(() => {
                let isAudit = app.globalData.isAudit,
                    isAuthed = app.globalData.isAuthed;

                this.setData({
                    isAudit,
                    isAuthed
                });
            });
    },

    onShow: function () {
        this.init();
    },

    init: function () {
        if (this.autoPublish) {
            this.scanCodePublish();
            this.autoPublish = false;
        }
    },

    scanCodePublish: function (e) {
        if (!this.data.isAuthed) {
            this.setData({
                showAuthDialog: true
            });
            return;
        }
        this.onScanCode();
    },

    onScanCode: function () {
        scanCode()
            .then(isbn => {
                wx.navigateTo({
                    url: `../housePublishList/housePublishList?isbn=${isbn}`
                });
            })
    },

    photo: function (e) {
        console.log(e)
    },

    onShareAppMessage: function (res) {
        return shareCodeHandler.getShareDetail();
    }
});